from datetime import datetime
from django.db import models
from django.contrib.auth.models import User
from photologue.models import Photo
# attempt to load the django-tagging TagField from default location,
# otherwise we substitude a dummy TagField.
try:
    from tagging.fields import TagField
    tagfield_help_text = 'Separate tags with spaces, put quotes around multiple-word tags.'
except ImportError:
    class TagField(models.CharField):
        def __init__(self, **kwargs):
            default_kwargs = {'max_length': 255, 'blank': True}
            default_kwargs.update(kwargs)
            super(TagField, self).__init__(**default_kwargs)
        def get_internal_type(self):
            return 'CharField'
    tagfield_help_text = 'Django-tagging was not found, tags will be treated as plain text.'


class Video(models.Model):
    original_video = models.FileField('original video', upload_to='videos/original/')
    video = models.FileField('video', upload_to='videos/converted/')
    title = models.CharField('title', max_length=100, unique=True)
    title_slug = models.SlugField('slug', unique=True,
                                  help_text='A "slug" is a unique URL-friendly title for an object.')
    caption = models.TextField('caption', blank=True)
    date_added = models.DateTimeField('date added', default=datetime.now, editable=False)
    is_public = models.BooleanField('is public', default=True, help_text='Public videos will be displayed in the default views.')
    tags = TagField(help_text=tagfield_help_text, verbose_name='tags')
    member = models.ForeignKey(User, related_name="added_videos", blank=True, null=True)
    image = models.ForeignKey(Photo, blank=True, null=True)
    class Meta:
        ordering = ['-date_added']
        get_latest_by = 'date_added'
        verbose_name = "video"
        verbose_name_plural = "videos"

    def __unicode__(self):
        return self.title

    def __str__(self):
        return self.__unicode__()
